<?php
set_time_limit(120);
require_once("../../inc/global.php");
require_once("../MenuSys/function.php");
if(!isset($_GET['type']) || !isset($_GET['id'])){
	adminislogin();
}

/**
 创建目录
 */
function mkdirm($path){ 
	if (!file_exists($path)){ 
		mkdirm(dirname($path)); 
		mkdir($path, 0777); 
	} 
}

function listboxsubs($list,$id,$gang,$m){
	if(is_array($list)){
		if($gang==0){
			$gangstring="";
		}else{
			$gangstring='';
			for($j=1;$j<=$gang;$j++){
				if($j==$gang)
				{
					$gangstring=$gangstring."　|-";
				}
				else
				{
					$gangstring=$gangstring."　|";
				}
			}
		}
		$optionitem='';
		for($i=0;$i<count($list);$i++){
			if($list[$i][2]==$id){
				$optionitem.="<option value=\"".$m.'_'.$list[$i][0]."\">".$gangstring.$list[$i][1]."</option>\n";
				$optionitem.=listboxsubs($list,$list[$i][0],($gang+1),$m);
			}
		}
		return $optionitem;
	}
}
$data=$db->getlistarr('select * from menusys where menusys0>3');
ob_start();
$html->includetop("","style=\"width:98%\";");
if(isset($_GET['type']) && isset($_GET['id'])){
	$globalurl = 'http://'.$_SERVER['HTTP_HOST'].'/'.ADMINROOT.'/System/htmlSys/createhtml.php';
	$type=$_GET['type'];
	$id=$_GET['id'];
	$id=explode('_',$id);
	$m=null;
	foreach($data as $val){
		if(intval($val['menusys0'])==intval($id[0])){
			$m=$val;
			break;
		}
	}
	
	$table=$m['menusys8'];
	$pathinfo=pathinfo(SITEROOT.$m['menusys19']);
	if($type=='type1'){ # 单页静态
	    $menuid = $_GET['id'];
		$menuconfig = array();
		foreach($data as $val){
			if(intval($val['menusys0'])==intval($menuid)){
				$menuconfig=$val;
				break;
			}
		}
		
		$url=$globalurl . '?type=onepage&menuid='.$menuid;
		if((!empty($menuconfig['menusys13']) && !empty($menuconfig['menusys19'])) || (!empty($menuconfig['mmenusys13']) && !empty($menuconfig['mmenusys19']))){

			$status=get_headers($url,1);
			if(strpos($status[0],'200')){
				## PC端;
				if(!empty($menuconfig['menusys13']) && !empty($menuconfig['menusys19'])){
					if($status['PC-Status']=='ok'){
						echo '<a href="http://'.$_SERVER['HTTP_HOST'].$status['PC-Static'].'" target="_blank">http://'.$_SERVER['HTTP_HOST'].$status['PC-Static'].'</a> <font color="#090">静态生成成功！</font>';
					}else{
						echo '<font color="red">静态生成失败！页面无法访问！</font>';
					}
					
				}
				
				## 移动端;
				if(!empty($menuconfig['mmenusys13']) && !empty($menuconfig['mmenusys19'])){
					if($status['M-Status']=='ok'){
						echo '<a href="http://'.$_SERVER['HTTP_HOST'].$status['M-Static'].'" target="_blank">http://'.$_SERVER['HTTP_HOST'].$status['M-Static'].'</a> <font color="#090">静态生成成功！</font>';
						//$s=$db->query('update '.$table.' set '.$table.'3="'.$status['M-Static'].'" where '.$table.'1='.$menuid. ' limit 1');
					}else{
						echo '<font color="red">静态生成失败！页面无法访问！</font>';
					}
				}
			}else{
				header('HTTP/1.1 500 Internal Server Error');
				echo '<font color="red">静态生成失败！页面无法访问！</font>';
			}
			
		}
	   
	   
		
	}elseif($type=='type2'){ # 列表页静态
		if($m['menusys14']==''){
			$htmlpagepath = $m['menusys19'].'/list{$cid}_{$page}.html';
		}else{
			$htmlpagepath = $m['menusys14'];
			
		}
		$filename = $htmlpagepath;
		$htmlpagepath=base64_encode(gzcompress($htmlpagepath,9));
	 	$htmlpagepath=strtr($htmlpagepath,array('/'=>'_'));
		
		$url=$globalurl . '?type=list&menuid=' . $id[0] . '&classid=' . $id[1] .'&htmlpagepath='.$htmlpagepath;

		$pages=1;
		for($i=1;$i<=$pages;$i++){
			$surl=$url . '&page=' . $i;
			$status=get_headers($surl);
			if(strpos($status[0],'200')){
				$data=file_get_contents($surl);
				$fname=strtr($filename,array('{$page}'=>$i,'{$cid}'=>$id[1]));
				$p=pathinfo(SITEROOT.$fname);
				mkdirm($p['dirname']);
				file_put_contents(SITEROOT.$fname,$data);
				$pages++;
				flush();
				ob_flush();
				echo '<a href="http://'.$_SERVER['HTTP_HOST'].$fname.'" target="_blank">http://'.$_SERVER['HTTP_HOST'].$fname.'</a> <font color="#090">静态生成成功！</font>';
				if(DEBUG) echo '<font style="color:#999999">'.$surl.'</font>';
				echo '<br />';
			}else{
				if($i==1){
					echo $surl;
					echo '<font color="red">生成失败！页面无法访问！</font><br />';
				}else{	
					echo '<font color="red">静态生成完成！</font><br />';
					break;	
				}
			}
		}
	}elseif($type=='type3'){   # 内容页静态
		
		$menuid=$_GET['id'];
		$menuconfig = array();
		foreach($data as $val){
			if(intval($val['menusys0'])==intval($menuid)){
				$menuconfig=$val;
				break;
			}
		}
		if((!empty($menuconfig['menusys16']) && !empty($menuconfig['menusys17'])) || (!empty($menuconfig['mmenusys16']) && !empty($menuconfig['mmenusys17']))){
			
			if(isset($_GET['contentid'])){
				
				$url = $globalurl.'?type=detail&menuid='.$menuid.'&id='.$_GET['contentid'];
				
				$status=get_headers($url,1);
				if(strpos($status[0],'200')){
					
					## PC端;
					if(!empty($menuconfig['menusys16']) && !empty($menuconfig['menusys17'])){
						if($status['PC-Status']=='ok'){
							echo '<a href="http://'.$_SERVER['HTTP_HOST'].$status['PC-Static'].'" target="_blank">http://'.$_SERVER['HTTP_HOST'].$status['PC-Static'].'</a> <font color="#090">静态生成成功！</font>';
							//$s=$db->query('update '.$table.' set '.$table.'2="'.$status['PC-Static'].'" where '.$table.'0='.$_GET['contentid']. ' limit 1');
						}else{
							echo '<font color="red">静态生成失败！页面无法访问！</font>';
						}
					}
					
					## 移动端;
					if(!empty($menuconfig['mmenusys16']) && !empty($menuconfig['mmenusys17'])){
						if($status['M-Status']=='ok'){
							echo '<a href="http://'.$_SERVER['HTTP_HOST'].$status['M-Static'].'" target="_blank">http://'.$_SERVER['HTTP_HOST'].$status['M-Static'].'</a> <font color="#090">静态生成成功！</font>';
							//$s=$db->query('update '.$table.' set '.$table.'3="'.$status['M-Static'].'" where '.$table.'0='.$_GET['contentid']. ' limit 1');
						}else{
							echo '<font color="red">静态生成失败！页面无法访问！</font>';
						}
					}
				}else{
					header('HTTP/1.1 500 Internal Server Error');
					echo '<font color="red">静态生成失败！页面无法访问！</font>';
				}
				
			}else{
				header('HTTP/1.1 500 Internal Server Error');
				echo '<font color="red">静态生成失败！页面无法访问！</font>';
			}
			
		}else{
			header('HTTP/1.1 500 Internal Server Error');
			echo '<font color="red">静态生成失败！页面无法访问！</font>';
		}
	}
}else{



?>
<script type="text/javascript">
function jump(id,val){
	document.getElementById('createhtml').src='status.php?status=1';
	if(val==null){
		var val=document.getElementById(id).value;
	}
	document.getElementById('createhtml').src='?type='+id+'&id='+val;
	//window.location.href='?type='+id+'&id='+val;
}
</script>
<fieldset>
    <legend style="font-size:14px;">静态页面生成</legend>
    单页面生成：
    <select id="type1" name="type1">
    <option value="0">请选择..</option>
    <?php
	$tval=array();
    foreach($data as $key=>$val){
        if(strtolower($val['menusys8'])=='onepagesys'){
            echo '<option value="'.$val[0].'">'.$val[1].'</option>'."\n";
			$tval[]=$val[0];
        }
    }
	$tvalstr=implode(',',$tval);
    ?>
    </select>
    <input type="button" value=" 生成 " onclick="jump('type1')" />
    <input type="button" value=" 生成所有单页面 " onclick="jump('type1','<?php echo $tvalstr;?>')" />
    
    
    <br />
<br />
    列表页生成：
    <select id="type2" name="type2">
    <option value="0">请选择...</option>
    <?php
    foreach($data as $val){
        if(strtolower($val['menusys8'])!='onepagesys' && $val['menusys15']!=''){
            if($val['menusys18']==0){
                echo '<option value="'.$val[0].'">'.$val[1].'</option>'."\n";
            }else{
                echo '<option value="'.$val[0].'" style="font-weight:bold" disabled>'.$val[1].'</option>'."\n";
                $d=$db->getlistarr("select * from classsys where classsys4=".$val[18]);
                echo listboxsubs($d,0,1,$val[0]);
            }
        }
    }
    ?>
    </select>
    <input type="button" value=" 生成 " onclick="jump('type2')" />
    
    <br />
<br />
    内容页生成：
    <select id="type3" name="type3">
    <option value="0">请选择...</option>
    <?php
    foreach($data as $val){
        if(strtolower($val['menusys8'])!='onepagesys' && $val['menusys16']!=''){
                echo '<option value="'.$val[0].'">'.$val[1].'</option>'."\n";
        }
    }
    ?>
    </select>
    <input type="button" value=" 生成 " onclick="jump('type3')" />
    <br />
</fieldset>
<fieldset>
<legend style="font-size:14px;">静态页面生成</legend>
<iframe width="100%" height="220px" frameborder="0" id="createhtml" src="status.php?status=0" scrolling="auto"></iframe>
</fieldset>
<script type="text/javascript">try{document.getElementById('t').value='<?php echo $_GET['t'];?>';}catch(e){};</script>
<?php
}
$html->includeend();
?>
